<?php

namespace app\api\model;

use app\common\model\BaseModel;
use think\facade\Db;

use function fast\e;


class Match extends BaseModel
{


    // 表名
    protected $name = 'match';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;


    public function getList($user,$data)
    {
        $page =$data['page'] ?? 1; // 从请求中获取分页参数，默认为1
        $list = self::where(['status' => 1])
            ->order('sort asc')
            ->page($page, 5)->select();
        $order = Db::name('match_order')->where(['user_id' => $user['id'],'status'=>2])->column('match_id');

        foreach ($list as &$item) {
            if($item['enter_start_time'] > time()){
               $item['status_text'] = '未开始';
            }else if($item['enter_end_time'] < time()){
                $item['status_text'] = '已结束';
            }else if($item['enter_end_time'] > time() && $item['enter_start_time'] < time() && in_array($item['id'],$order)){
                $item['status_text'] = '已报名';
            }else if($item['enter_end_time'] > time() && $item['enter_start_time'] < time() && !in_array($item['id'],$order)){
                $item['status_text'] = '报名中';
            }else{
                $item['status_text'] = '';
            }
            $item['nums'] = Db::name('match_order')->where(['status' => 2,'match_id' => $item['id']])->count();

            $item['start_time'] = date('m/d H:i');

        }
        return ['status' => 1, 'message' => '操作成功', 'result' => $list];
    }


    public function getDetail($user, $params = array())
    {
        $detail = self::where(['id' => $params['id']])->find();
        if (!$detail) {
            return ['status' => 0, 'message' => '活动不存在', 'result' => ''];
        }
        if ($detail['status'] == 0) {
            return ['status' => 0, 'message' => '活动已下架', 'result' => ''];
        }

        $order = Db::name('match_order')->where(['user_id' => $user['id'],'status'=>2,'match_id' => $params['id']])->find();


        if($detail['enter_start_time'] > time()){
            $detail['status'] = 1;
        }else if($detail['enter_end_time'] < time()){
            $detail['status'] = 2;
        }else if($detail['enter_end_time'] > time() && $detail['enter_start_time'] < time() && $order){
            $detail['status'] = 3;
        }else if($detail['enter_end_time'] > time() && $detail['enter_start_time'] < time() && !$order){
            $detail['status'] = 4;
        }else{
            $detail['status'] = 5;
        }


        $detail['start_time'] = date('Y/m/d H:i', $detail['start_time']);
        $detail['enter_start_time'] = date('Y/m/d H:i', $detail['enter_start_time']);
        $detail['enter_end_time'] = date('Y/m/d H:i', $detail['enter_end_time']);

        return ['status' => 1, 'message' => '操作成功', 'result' => ['data' => $detail,'order' => $order]];
    }


    public function matchPay($data, $user)
    {
        $id = $data['id'] ?? 0;
        $phone = $data['phone'] ?? '';
        $username = $data['username'] ?? '';
        //查询活动
        $match = self::where(['id' => $id])->find();
        if (!$match) {
            return ['status' => 1, 'message' => '活动不存在', 'result' => ''];
        }
        if ($match['status'] == 0) {
            return ['status' => 1, 'message' => '活动已下架', 'result' => ''];
        }

        if ($match['enter_start_time'] > time()) {
            return ['status' => 1, 'message' => '活动未开始', 'result' => ''];
        }

        if (!$phone || !$username) {
            return ['status' => 1, 'message' => '请填写姓名或手机号', 'result' => ''];
        }

        if ($match['enter_end_time'] < time()) {
            return ['status' => 1, 'message' => '活动已结束', 'result' => ''];
        }
        //查询已报名人数
        $order = Db::name('match_order')->where(['match_id' => $id,'status'=>2])->count();
        if($order + 1 > $match['user_nums']){
            return ['status' => 1, 'message' => '人数已满', 'result' => ''];
        }

        //创建订单
        $res['title'] = $match['title'];
        $res['out_trade_no'] = self::getOutTradeNo();
        $res['user_id'] = $user['id'];
        $res['match_id'] = $data['id'];
        $res['pay_money'] = $match['price'];
        $res['username'] = $username;
        $res['desc'] = $data['desc'] ?? '';
        $res['phone'] = $phone ?? '';

        $res['create_time'] = time();
        $res['update_time'] = time();
        if (!Db::name('match_order')->insert($res)) {
            return ['status' => 1, 'message' => '下单失败', 'result' => ''];
        }
        $result = (new SaveIng())->Pay($res, $user, 'matchNotify', '报名活动');
        if ($result) {
            $result['out_trade_no'] = $res['out_trade_no'];
            return ['status' => 0, 'message' => '请求成功', 'result' => $result];
        } else {
            return ['status' => 1, 'message' => '下单失败', 'result' => ''];
        }
    }

    public static function getOutTradeNo()
    {
        $out_trade_no = sys_create_orderno(1);
        $order = Db::name('match_order')->where('out_trade_no', $out_trade_no)->count();
        if ($order) {
            self::getOutTradeNo();
        }

        return $out_trade_no;
    }


    public function getMatchOrderDetail($params,$user){
        $match_id = $params['match_id'] ?? 0;
        $matchOrder = Db::name('match_order')->where(['match_id' => $match_id,'user_id' => $user['id']])->find();
        if(!$matchOrder){
            return ['status' => 1, 'message' => '下单失败', 'result' => ''];
        }
        $matchOrder['pay_time'] = date('Y-m-d H:i:s',$matchOrder['pay_time']);
        return ['status' => 0, 'message' => '请求成功', 'result' => $matchOrder];

    }

}
